<?php


App::uses('Appcontroller', 'Controller');


class ImportController extends AppController {

	var $name = "Contact";


	//
	

	public function index() {

	}

	function upload() {
		if (empty($this->data)) {
			// $this->set('tags', $this->Post->Tag->generateList(null,null,null,null,"{n}.Tag.tag"));
			// $this->set('selectedTags', null);
			// $this->render();
		} else {
			// $this->cleanUpFields();
			
			// upload the file to the server
			$tableName = $this->data['dbtable'];

			// echo $tableName;

			$fileOK = $this->uploadFiles('files', $this->data['xls']);
			
			// print out data
			// $this->pa($this->data);

			// echo $fileOK['urls'][0];

			$row = 1;
			if (($handle = fopen($fileOK['urls'][0], "r")) !== FALSE) {
				while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) {
					$num = count($data);
					if($row != 1 && $data[12] != null) {

						$this->Contact->create();
						// $this->Import->create();
						$this->request->data['Contact']['name'] = $data[1];
						$this->request->data['Contact']['position'] = $data[2];
						$this->request->data['Contact']['email'] = $data[3];
						$this->request->data['Contact']['mobile'] = $data[4];
						$this->request->data['Contact']['home'] = $data[5];
						$this->request->data['Contact']['office'] = $data[6];
						$this->request->data['Contact']['address'] = $data[7];
						$this->request->data['Contact']['notes'] = $data[8];
						$this->request->data['Contact']['category'] = $data[9];
						$this->request->data['Contact']['taken_from'] = $data[10];
						$this->request->data['Contact']['create_time'] = $data[11];
						$this->request->data['Contact']['modified_date'] = $data[12];
						// $this->request->data['Contact']['dept_of_cont'] = $data[13];

						if($this->Contact->save($this->request->data)) {
							// echo "SUCCESSSSS!";
						} else {
							// echo "ERRORRRRRRRRRRR!" ;
						}


					}
					$row++;
					
				}
				fclose($handle);
			}



			
			// $this->pa($this->data['xls']);

			// $this->pa($this->data['xls']['type']);
			
		}
	}

}

?>